<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of LoginEgresados
 *
 * @author JeyDotC
 * 
 * @export Modules
 */
class Login extends Module {

    private $userName = "";
    private $password = "";
    private $returnURL = "";

    public function __construct() {
        parent::__construct();
    }

    function index() {
        $this->returnURL = $this->request->get("return", $this->getDefaultReturnURL());

        $this->assign("action", $this->route->action("doLogin"), array("return" => $this->returnURL,));

        $this->assign("lastUsername", $this->userName);
        $this->display("index");
    }

    function doLogin() {
        if ($this->request->submitted()) {
            $this->userName = $this->request->post("Username");
            $this->password = $this->request->post("Password");
            $this->returnURL = base64_decode($this->request->get("return", $this->getDefaultReturnURL()));

            $result = $this->authenticate();

            if ($result) {
                $this->route->redirect($this->returnURL . "&NoFrame=1");
            } else {
                $this->addMessage(new Message(
                                "La informaci&oacute;n de nombre de usuario o contrase&ntilde;a introducida no es correcta",
                                "",
                                Message::ERROR
                ));
            }
        }

        $this->index();
    }

    private function authenticate() {
        /* @var $mainframe JApplication */
        global $mainframe;

        $credentials = array("username" => $this->userName, "password" => $this->password,);

        return $mainframe->login($credentials, array("silent" => true));
    }

    public function getDefaultReturnURL() {
        return base64_encode($this->route->appAction("Acaweb", "ActualizarDatos"));
        ;
    }

}

?>
